package com.study.zookeeperAPI;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * 同步创建一个节点
 */
public class CreateNodesSync implements Watcher {

    private static CountDownLatch latch = new CountDownLatch(1);

    public static void main(String[] args) {

        String connectStr = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2184";
        try{
            ZooKeeper zooKeeper = new ZooKeeper(connectStr,5000,new CreateNodesSync());
            latch.await();
            String path1 = zooKeeper.create("/zk-test-ephemeral-","1234".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            System.out.println("Success create znode: " + path1);

            String path2 = zooKeeper.create("/zk-test-ephemeral-","1234".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
            System.out.println("Success create znode:" + path2);

        }catch (IOException e){

        }catch (InterruptedException e){

        }catch (KeeperException e){

        }

    }



    public void process(WatchedEvent watchedEvent) {

        if(Event.KeeperState.SyncConnected == watchedEvent.getState()){
            latch.countDown();
        }

    }
}
